* Table A20: Sensitivity of Mobility Estimates to Number of Income Measures in Average

clear
use "${data_dir}DavisMazumderData.dta"

keep if age_firstSurvey<=18
drop if (mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) 
 



drop faminc0 faminc1

egen faminc0_1 = rowmean(parent1_min1500)
egen faminc1_1 = rowmean(kid1_min1500)
 gen lfaminc0_1 = log(faminc0_1)
 gen lfaminc1_1 = log(faminc1_1)
 gen loginc66_1 = lfaminc0_1*(surv79==0)
 gen loginc79_1 = lfaminc0_1*(surv79==1)

egen faminc0_2 = rowmean(parent1_min1500 parent2_min1500 )
egen faminc1_2 = rowmean(kid1_min1500 kid2_min1500)
 gen lfaminc0_2 = log(faminc0_2)
 gen lfaminc1_2 = log(faminc1_2)
 gen loginc66_2 = lfaminc0_2*(surv79==0)
 gen loginc79_2 = lfaminc0_2*(surv79==1)

egen faminc0_3 = rowmean(parent1_min1500 parent2_min1500 parent3_min1500)
egen faminc1_3 = rowmean(kid1_min1500 kid2_min1500 kid3_min1500)
 gen lfaminc0_3 = log(faminc0_3)
 gen lfaminc1_3 = log(faminc1_3)
 gen loginc66_3 = lfaminc0_3*(surv79==0)
 gen loginc79_3 = lfaminc0_3*(surv79==1)

forv i=1/3 {
	
gen N = .
 tab n  if surv79==0 & women==0 & faminc0_`i'<. & faminc1_`i'<. [w=weight]
 replace N = r(N) if surv79==0 & women==0 & faminc0_`i'<. & faminc1_`i'<.
 tab n  if surv79==0 & women==1  & faminc0_`i'<. & faminc1_`i'<. [w=weight]
 replace N = r(N) if surv79==0 & women==1 & faminc0_`i'<. & faminc1_`i'<.
 tab n  if surv79==1 & women==0  & faminc0_`i'<. & faminc1_`i'<. [w=weight]
 replace N = r(N) if surv79==1 & women==0 & faminc0_`i'<. & faminc1_`i'<.
 tab n  if surv79==1 & women==1 & faminc0_`i'<. & faminc1_`i'<. [w=weight]
 replace N = r(N) if surv79==1 & women==1 & faminc0_`i'<. & faminc1_`i'<.
 
 
* Family Income Rank in Parent Generation
gen faminc0_nm = faminc0_`i'
replace faminc0_nm = . if missing(faminc0_`i')  | missing(faminc1_`i')
set sortseed 6688571
sort women surv79 faminc0_nm
by women surv79: gen __rank0 = sum(weight) if  !missing(faminc0_`i') & !missing(faminc1_`i')
gen _rank0 = __rank0/N if  !missing(faminc0_`i') & !missing(faminc1_`i')
by women surv79 faminc0_nm: egen rank0_`i' = mean(_rank0) if  !missing(faminc0_`i') & !missing(faminc1_`i')
 replace rank0_`i' = 100*rank0_`i'
gen perc66_`i' = rank0_`i'*(1-surv79)
gen perc79_`i' = rank0_`i'*surv79

 

 
* Family Income Rank in Kid Generation
gen faminc1_nm = faminc1_`i'
 replace faminc1_nm = . if missing(faminc0_`i')  | missing(faminc1_`i')
set sortseed 6688571 
sort women surv79 faminc1_nm
by women surv79: gen __rank1 = sum(weight) if  !missing(faminc0_`i') & !missing(faminc1_`i')
gen _rank1 = __rank1/N if !missing(faminc0_`i') & !missing(faminc1_`i')
bys women surv79 faminc1_nm: egen rank1_`i' = mean(_rank1) if !missing(faminc0_`i') & !missing(faminc1_`i')
replace rank1_`i' = 100*rank1_`i'

drop N faminc0_nm faminc1_nm __rank0 __rank1 _rank0 _rank1 
} 

forv i=1/3 {
* --------- Rank-Rank --------- *

* Pooled
reg rank1_`i' perc66_`i' perc79_`i' surv79 women [w=weight] , cluster(hhid)
eststo rank_p_`i'
test perc79_`i'=perc66_`i'
estadd scalar p = r(p): rank_p_`i'
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_p_`i'
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_p_`i'
drop s
lincom perc79_`i'-perc66_`i'
estadd scalar diff = r(estimate): rank_p_`i'
estadd scalar se = r(se): rank_p_`i'
global rank_main_est = r(estimate)
global rank_main_se = r(se)


* Women
reg rank1_`i' perc66_`i' perc79_`i' surv79 if women==1 [w=weight] , cluster(hhid)
eststo rank_w_`i'
test perc79_`i'=perc66_`i'
estadd scalar p = r(p): rank_w_`i'
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_w_`i'
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_w_`i'
lincom perc79_`i'-perc66_`i'
estadd scalar diff = r(estimate): rank_w_`i'
estadd scalar se = r(se): rank_w_`i'
drop s

* Men
reg rank1_`i' perc66_`i' perc79_`i' surv79 if women==0 [w=weight] , cluster(hhid)
eststo rank_m_`i'
test perc79_`i'=perc66_`i'
estadd scalar p = r(p): rank_m_`i'
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_m_`i'
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_m_`i'
lincom perc79_`i'-perc66_`i'
estadd scalar diff = r(estimate): rank_m_`i'
estadd scalar se = r(se): rank_m_`i'
drop s



* --------- IGE --------- *

* Pooled
reg lfaminc1_`i' loginc66_`i' loginc79_`i' surv79 women [w=weight], cluster(hhid)
eststo IGE_p_`i'
test loginc79_`i'=loginc66_`i'
estadd scalar p = r(p): IGE_p_`i'
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_p_`i'
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_p_`i'
drop s
lincom loginc79_`i'-loginc66_`i'
estadd scalar diff = r(estimate): IGE_p_`i'
estadd scalar se = r(se): IGE_p_`i'
global ige_main_est = r(estimate)
global ige_main_se = r(se)

* Women
reg lfaminc1_`i' loginc66_`i' loginc79_`i' surv79 if women==1 [w=weight], cluster(hhid)
eststo IGE_w_`i'
test loginc79_`i'=loginc66_`i'
estadd scalar p = r(p): IGE_w_`i'
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_w_`i'
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_w_`i'
lincom loginc79_`i'-loginc66_`i'
estadd scalar diff = r(estimate): IGE_w_`i'
estadd scalar se = r(se): IGE_w_`i'
drop s

* Men
reg lfaminc1_`i' loginc66_`i' loginc79_`i' surv79 if women==0 [w=weight], cluster(hhid)
eststo IGE_m_`i'
test loginc79_`i'=loginc66_`i'
estadd scalar p = r(p): IGE_m_`i'
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_m_`i'
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_m_`i'
lincom loginc79_`i'-loginc66_`i'
estadd scalar diff = r(estimate): IGE_m_`i'
estadd scalar se = r(se): IGE_m_`i'
drop s

}

#delimit ;
estout rank_p_1 IGE_p_1 rank_w_1 IGE_w_1 rank_m_1 IGE_m_1
	using  "${results_dir}tableA20.txt", replace
	keep(perc66_1 perc79_1) 
	rename(loginc66_1  perc66_1 loginc79_1 perc79_1)
	cells(b(star fmt(3))  se(par fmt(3))) 
	collabels(,none) stat(diff se p  N66 N79) stardrop(*) ;	

estout rank_p_2 IGE_p_2 rank_w_2 IGE_w_2 rank_m_2 IGE_m_2
	using  "${results_dir}tableA20.txt", append
	keep(perc66_2 perc79_2) 
	rename(loginc66_2  perc66_2 loginc79_2 perc79_2)
	cells(b(star fmt(3))  se(par fmt(3))) 
	collabels(,none) stat(diff se p  N66 N79) stardrop(*) ;		
	
	
estout rank_p_3 IGE_p_3 rank_w_3 IGE_w_3 rank_m_3 IGE_m_3
	using  "${results_dir}tableA20.txt", append
	keep(perc66_3 perc79_3) 
	rename(loginc66_3  perc66_3 loginc79_3 perc79_3)
	cells(b(star fmt(3))  se(par fmt(3))) 
	collabels(,none) stat(diff se p  N66 N79) stardrop(*) ;		
	
#delimit cr	



